//
//  ZCSListenListDetailViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "ZCSListenListDetailViewController.h"
#import "ZCSListenListDetailModel.h"
#import "ZCSListenListListModel.h"
#import "ZCSTipsReTableViewCell.h"
#import "ZCSListenListSoundTableViewCell.h"
#import "ZCSReHeaderTableViewCell.h"
#import "ZCSListenHeaderView.h"
#import "AlbumDetailViewController.h"
#import "ZCSPlayMusicViewController.h"
#import "LTDataBase.h"

@interface ZCSListenListDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;

@property (nonatomic, retain) ZCSListenListDetailModel *infoModel;

@property (nonatomic, assign) BOOL isCollect;

@property (nonatomic, retain) UIBarButtonItem *collect;

@end

@implementation ZCSListenListDetailViewController

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [self removeObserver:self forKeyPath:@"isCollect"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"听单";
    self.collect = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"noCollect"] imageWithRenderingMode:1] style:UIBarButtonItemStylePlain target:self action:@selector(didClickCollect:)];
    self.navigationItem.rightBarButtonItem = self.collect;
    self.dataArray = [NSMutableArray array];
    [self creatView];
    [self getDataWith:self.ID];
    [self addObserver:self forKeyPath:@"isCollect" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)didClickCollect:(UIBarButtonItem *)button
{
    if (_isCollect == NO) {
        self.collect.image = [[UIImage imageNamed:@"collect"] imageWithRenderingMode:1];
        self.isCollect = YES;
        [MBProgressHUD showSuccess:@"已添加收藏"];
        LTDataBase *data = [LTDataBase shareLTDataBaseTool];
        [data insertDataWithVoiceModel:self.infoModel];
        
    }
    else
    {
        self.collect.image = [[UIImage imageNamed:@"noCollect"] imageWithRenderingMode:1];
        self.isCollect = NO;
        [MBProgressHUD showSuccess:@"已取消收藏"];
         LTDataBase *data = [LTDataBase shareLTDataBaseTool];
        [data delegateDataWithModel_id:self.infoModel.specialId type:1];
    }
    
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    NSInteger num = [data isSaveDataInTableWithModel_id:self.ID type:1];
    if (num == 0) {
        self.isCollect = YES;
    }
    else
    {
        self.isCollect = NO;
    }
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([[change objectForKey:@"new"] boolValue] == NO) {
        self.collect.image = [[UIImage imageNamed:@"noCollect"] imageWithRenderingMode:1];
    }
    else
    {
        self.collect.image = [[UIImage imageNamed:@"collect"] imageWithRenderingMode:1];
    }
}

- (void)creatView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.sectionFooterHeight = 50 * FITHEIGHT;
    self.tableView.separatorStyle = 0;
    
    [self.view addSubview:_tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSListenListSoundTableViewCell" bundle:nil] forCellReuseIdentifier:@"ListenListCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSTipsReTableViewCell" bundle:nil] forCellReuseIdentifier:@"TipCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSReHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"reHeader"];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //专辑
    if ([self.type isEqualToString:@"1"]) {
        ZCSTipsReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TipCell"];
        cell.listenListModel = [self.dataArray objectAtIndex:indexPath.row];
        return cell;
    }
    //声音
    else
    {
        ZCSListenListSoundTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListenListCell"];
        cell.model = [self.dataArray objectAtIndex:indexPath.row];
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   ZCSListenListListModel *model = [self.dataArray objectAtIndex:indexPath.row];
    if ([self.type isEqualToString:@"1"]) {
        AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
        advc.hidesBottomBarWhenPushed = YES;
        advc.albmId = model.id;
        [self.navigationController pushViewController:advc animated:YES]; 
    }
    else
    {
        ZCSPlayMusicViewController *pmvc = [ZCSPlayMusicViewController sharePlayVC];
        pmvc.maxPage = 1;
        pmvc.dataArray = self.dataArray;
        pmvc.musicIndex = indexPath.row;
        
        [self presentViewController:pmvc animated:YES completion:nil];
    }
}


- (void)creatTableHeaderViewWith:(ZCSListenListDetailModel *)model
{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName, nil];
    CGRect newRect = [model.intro boundingRectWithSize:CGSizeMake(SCREENWIDTH - 20 * FITWIDTH, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    UIView *headerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, newRect.size.height + 180 * FITHEIGHT)];
    ZCSListenHeaderView *header = [[ZCSListenHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, newRect.size.height + 170 * FITHEIGHT)];
    header.model = model;
    [headerBackView addSubview:header];
    self.tableView.tableHeaderView = headerBackView;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCSReHeaderTableViewCell *header = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
    header.headerLabel.text = @"听单列表";
    header.backgroundColor = [UIColor whiteColor];
    header.headerLabel.textColor = [UIColor blackColor];
    header.moreLabel.hidden = YES;
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * FITHEIGHT;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.type isEqualToString:@"1"]) {
        return 90 * FITHEIGHT;
    }
    return 100 * FITHEIGHT;
}

- (void)getDataWith:(NSInteger)ID
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=android&id=%ld", ID];
    __weak ZCSListenListDetailViewController *ldvc = self;
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:0 Success:^(id result) {
        ldvc.infoModel = [[ZCSListenListDetailModel alloc] initWithDic:result[@"info"]];
        for (NSDictionary *dic in result[@"list"]) {
            ZCSListenListListModel *model = [[ZCSListenListListModel alloc] initWithDic:dic];
            [ldvc.dataArray addObject:model];
        }
        [ldvc creatTableHeaderViewWith:ldvc.infoModel];
        [ldvc.tableView reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
